<?php
class SearchModel extends Model {
	public function __construct() {
		parent::__construct('Article');
	}
	
	public function searchByKey($key, $pageSize = 10) {
		import('ORG.Util.Page');
		$pre = C('DB_PREFIX');
		
		$where = "title like '%{$key}%' or tags like '%{$key}%'";
		$count = $this->where($where)->count();	
		if(empty($count)) $count = 0;
		$page = new Page($count, $pageSize);
		$pageString = $page->show();
		
		$sql = 'select id,title,tags,description,posttime,viewnum,viewimage'.
			" from {$pre}article".
			" where ({$where}) and id<=(select id from {$pre}article where {$where} order by id desc limit {$page->firstRow},1) order by id desc limit {$pageSize}";
			
		$data = $this->query($sql);
		//var_dump($this->getLastsql()); exit;
		
		$data['pageString'] = $pageString;
		
		return $data;
	}
}